package com.ghh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ghh.constant.MessageConstant;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.Company;
import com.ghh.service.CompanyService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Reference
    private CompanyService companyService;

    //查询所有地址
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Company> list = companyService.findAll();
            return new Result(true, MessageConstant.GET_COMPANY_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_COMPANY_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult addAddress(@RequestBody QueryPageBean queryPageBean){
        try {
            return companyService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    //添加
    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Company company ){
        try {
            companyService.addAddress(company);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_COMPANY_FAIL);
        }
        return new Result(true,MessageConstant.ADD_COMPANY_SUCCESS);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            companyService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
    }


}
